﻿Imports System.Text
Imports Global_Visas_Business_Layer
Imports System.ComponentModel
Imports System.Data

Partial Public Class frmUnassignedCases

    Private _NotSubscribed As Boolean = True

    Private Sub LoadConsultants(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpDept.SelectionChanged
        With Me.drpAssignCon
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, drpDept.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub


    Private Sub LoadDepartment(ByVal sender As Object, ByVal e As System.EventArgs) Handles drpBranch.SelectionChanged


        With Me.drpDept
            .DisplayMemberPath = "Department_Name"
            .SelectedValuePath = "Department_ID"
            .ItemsSource = CType(CommonDataFunctions.DepartmentList(Me.drpBranch.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub
    Private Sub UnassignedCases_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded

        With Me.drpConName
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(CommonDataFunctions.ConsultantList(True), 0, "Unassigned"), IListSource).GetList
            .SelectedIndex = 0
        End With

        With Me.drpBranch
            .DisplayMemberPath = "Branch_Name"
            .SelectedValuePath = "Branch_ID"
            .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
            .SelectedIndex = 0
        End With

        With Me.drpBranchCon
            .DisplayMemberPath = "Branch_Name"
            .SelectedValuePath = "Branch_ID"
            .ItemsSource = CType(CommonDataFunctions.BranchList(), IListSource).GetList
        End With


        Me.LoadConsultants(Nothing, New System.EventArgs)
        Dim branchid As Integer = CommonDataFunctions.GetLoggedOnConsultantBranchID

        drpBranch.SelectedValue = branchid
        drpBranchCon.SelectedValue = branchid
        
        Me.LoadCases(False, 1)
        Me.WireUpEvents()


    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub LoadCases(ByVal bIgnoreProducts As Boolean, Optional ByVal Branchid As Integer = 0)
        Dim dt As DataTable = Nothing
        Dim ConType As Admin.ConsultantType
        Dim ConStatus As Admin.ConsultantStatus
        Dim ProductTypeID As Integer


        ConStatus = Admin.ConsultantStatus.Unassigned

        'If Me.radViewAccMan.isChecked Then
        'ConType = Admin.ConsultantType.AccountManager
        If Me.radViewCW.IsChecked Then
            ConType = Admin.ConsultantType.CaseWorker
        ElseIf Me.radViewSales.IsChecked Then
            ConType = Admin.ConsultantType.SalesCon
        ElseIf Me.radCaseCoordinator.IsChecked Then
            ConType = Admin.ConsultantType.CaseCoordinator
        End If


        If bIgnoreProducts = False Then
            'drpProductTypes.Items.Clear()
            'If Branchid > 0 Then
            dt = Admin.GetUnassignedProductTypes(ConType, ConStatus, Me.drpConName.SelectedValue)
            'End If
            If Not dt Is Nothing Then
                With drpProductTypes
                    .DisplayMemberPath = "PRODUCTTYPE_NAME"
                    .SelectedValuePath = "PRODUCTTYPE_ID"
                    .ItemsSource = CType(CommonDataFunctions.AddExtraRowToDataTable(dt, 0, "All Product Types"), IListSource).GetList
                    .SelectedIndex = 1
                End With
            End If
            ProductTypeID = Me.drpProductTypes.SelectedValue
        End If

        Me.grdCases.ItemsSource = Admin.GetUnassignedCases(ConType, ConStatus, 0, Me.drpConName.SelectedValue, drpProductTypes.SelectedValue)

        Me.grdCases.SelectedItems.Clear()

        If Not IsNothing(Me.grdCases.ItemsSource) Then
            Me.lblRecords.Content = CType(Me.grdCases.ItemsSource, DataTable).Rows.Count
        End If

        'Me.WireUpEvents()

    End Sub

    Private Sub ViewChanged(ByVal sender As Object, ByVal e As System.EventArgs) ' Handles drpConName.SelectedIndexChanged, drpDepartment.SelectedIndexChanged, radConActive.CheckedChanged, radConInactive.CheckedChanged, radConUnassigned.CheckedChanged, radViewAccMan.CheckedChanged, radViewCW.CheckedChanged, radViewSales.CheckedChanged

        'MsgBox(CType(sender, RadioButton).Name)

        Me.UnwireUpEvents()
        Me.Cursor = Cursors.Wait
        Me.LoadCases(True)
        Me.Cursor = Cursors.Arrow
        Me.lvwSelected.Items.Clear()
        Me.WireUpEvents()
        ' blnLoadCases = True
    End Sub

    Private Sub WireUpEvents()

        'Do this after form loads has it fires about 10 times
        AddHandler drpConName.SelectionChanged, AddressOf ViewChanged
        AddHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged
        'AddHandler drpBranch.SelectedIndexChanged, AddressOf ViewChanged
        'AddHandler radConActive.CheckedChanged, AddressOf ViewChanged
        'AddHandler radConInactive.CheckedChanged, AddressOf ViewChanged
        'AddHandler radConUnassigned.CheckedChanged, AddressOf ViewChanged
        'AddHandler radViewAccMan, AddressOf ViewChanged
        AddHandler radViewCW.Checked, AddressOf ViewChanged
        AddHandler radViewCW.Unchecked, AddressOf ViewChanged
        AddHandler radViewSales.Checked, AddressOf ViewChanged
        AddHandler radViewSales.Unchecked, AddressOf ViewChanged
        AddHandler radCaseCoordinator.Checked, AddressOf ViewChanged
        AddHandler radCaseCoordinator.Unchecked, AddressOf ViewChanged
        AddHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged

        'AddHandler chkNoProd.CheckedChanged, AddressOf ViewChanged
        'AddHandler chkRetired.CheckedChanged, AddressOf ViewChanged

        'AddHandler chkNoProd.CheckedChanged, AddressOf chkNoProd_CheckedChanged
        'AddHandler chkAllCon.CheckedChanged, AddressOf chkAllCon_CheckedChanged
        'AddHandler chkAllDep.CheckedChanged, AddressOf chkAllDep_CheckedChanged
        'AddHandler chkRetired.CheckedChanged, AddressOf chkRetired_CheckedChanged

    End Sub

    Private Sub UnwireUpEvents()

        'Do this after form loads has it fires about 10 times
        RemoveHandler drpConName.SelectionChanged, AddressOf ViewChanged
        RemoveHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged
        RemoveHandler radViewCW.Checked, AddressOf ViewChanged
        RemoveHandler radViewCW.Unchecked, AddressOf ViewChanged
        RemoveHandler radViewSales.Checked, AddressOf ViewChanged
        RemoveHandler radViewSales.Unchecked, AddressOf ViewChanged
        RemoveHandler radCaseCoordinator.Checked, AddressOf ViewChanged
        RemoveHandler radCaseCoordinator.Unchecked, AddressOf ViewChanged
        RemoveHandler drpProductTypes.SelectionChanged, AddressOf ViewChanged


    End Sub


    Private Sub grdCases_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.grdCases.Columns.Count > 0 Then

            Dim myWidth = Me.grdCases.Width - (80 + 100 + 100)

            If myWidth > 0 Then

                'Me.grdCases.Columns(0).Width = CType(80, System.Windows.GridLength)
                'Me.grdCases.Columns(1).Width = 80
                'Me.grdCases.Columns(2).Width = myWidth * 0.14
                'Me.grdCases.Columns(3).Width = myWidth * 0.14
                'Me.grdCases.Columns(4).Width = myWidth * 0.14
                'Me.grdCases.Columns(5).Width = myWidth * 0.1
                'Me.grdCases.Columns(6).Width = 100
                'Me.grdCases.Columns(7).Width = myWidth * 0.1
                'Me.grdCases.Columns(8).Width = 100
                'Me.grdCases.Columns(9).Width = 100
                'Me.grdCases.Columns(10).Width = 80
            End If
        End If
    End Sub

    Private Sub grdCases_SelectedRowsChanged(ByVal sender As Object, ByVal e As EventArgs) Handles grdCases.SelectionChanged
        If _NotSubscribed Then

            'AddHandler System.Windows.Forms.Application.Idle, AddressOf Application_Idle
            AddHandler grdCases.SelectionChanged, AddressOf Application_Idle

            _NotSubscribed = False

            lblEvent.Content = ""
            If Me.grdCases.SelectedItems.Count > 0 Then

                Me.btnAssign.IsEnabled = True
                Me.btnDelete.IsEnabled = True
            Else
                Me.btnAssign.IsEnabled = False
                Me.btnDelete.IsEnabled = False
            End If
        End If
    End Sub


    Private Sub Application_Idle(ByVal sender As Object, ByVal e As EventArgs)

        'Stops code firing every time row is selected, only fires when all selected rows have fired

        RemoveHandler grdCases.SelectionChanged, AddressOf Application_Idle
        _NotSubscribed = True
        'System.Diagnostics.Debug.WriteLine(grdCases.SelectedRows.Count.ToString() + " rows selected")
        'MsgBox(grdCases.SelectedRows.Count.ToString)
        Me.SelectedCases()


    End Sub

    Private Sub SelectedCases()

        Dim myTable As New DataTable
        Dim myRow As DataRow
        Dim myGridRow As DataRow
        Dim i As Integer

        With myTable.Columns
            .Add(New DataColumn("Case ID", System.Type.GetType("System.Int32")))
            .Add(New DataColumn("Candidate", System.Type.GetType("System.String")))
            .Add(New DataColumn("Nationality", System.Type.GetType("System.String")))
            .Add(New DataColumn("Residence", System.Type.GetType("System.String")))
            .Add(New DataColumn("Product", System.Type.GetType("System.String")))
            .Add(New DataColumn("Destination", System.Type.GetType("System.String")))
        End With

        For i = 0 To Me.grdCases.SelectedItems.Count - 1


            'If Me.grdCases.SelectedRows(i).Location = Xceed.Grid.RowLocation.Data Then

            ' For Each myGridRow In Me.grdCases.SelectedRows
            myGridRow = grdCases.SelectedItems(i)
            myRow = myTable.NewRow

            With myRow
                .Item("Case ID") = myGridRow.Item("Case_ID").ToString
                .Item("Candidate") = myGridRow.Item("Candidate").ToString
                .Item("Nationality") = myGridRow.Item("Nationality").ToString
                .Item("Residence") = myGridRow.Item("Residence").ToString
                .Item("Product") = myGridRow.Item("Product").ToString
                .Item("Destination") = myGridRow.Item("Destination").ToString
            End With

            myTable.Rows.Add(myRow)
            'End If

        Next

        DataBinding.BindListView(myTable, Me.lvwSelected)

    End Sub

    Private Sub lvwSelected_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Me.lvwSelected.SelectedItems.Count > 0 Then
            Me.lblEvent.Content = TasksEvents.GetWebCommentsEvent(Me.lvwSelected.SelectedItems(0).SubItems(0).Text)
        End If

    End Sub

    Private Sub btnAssign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssign.Click

        Dim myCases() As Long = Nothing
        Dim i As Integer
        Dim myRow As DataRowView

        If Me.grdCases.SelectedItems.Count = 0 Then
            MsgBox("No Cases selected", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If
        Try
            If Me.drpAssignCon.SelectedIndex = -1 Then
                MsgBox("No consultant selected", MsgBoxStyle.Exclamation, "Error")
                Me.drpAssignCon.Focus()
                Exit Sub
            End If

            For Each myRow In Me.grdCases.SelectedItems

                ReDim Preserve myCases(i)

                myCases(i) = myRow.Item("Case_ID").ToString
                i += 1
            Next

            If myCases.Length > 0 Then
                If Me.radViewCW.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.CaseWorker, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        Me.ViewChanged(Me, New System.EventArgs)
                    End If
                ElseIf Me.radViewSales.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.SalesCon, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        Me.ViewChanged(Me, New System.EventArgs)
                    End If
                ElseIf Me.radCaseCoordinator.IsChecked Then
                    If Admin.AssignCaseworker(myCases, Admin.ConsultantType.CaseCoordinator, Me.drpAssignCon.SelectedValue, Me.chkMoveTasks.IsChecked) Then
                        Me.ViewChanged(Me, New System.EventArgs)
                    End If
                End If
            End If
        Catch ex As Exception
            CommonFuncs.ErrorHandler(ex, "frmUnassignedCases.btnAssign_Click")
        Finally
        End Try
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Only do this if this is an unassigned case

        'Dim myCases() As Long
        Dim myRow As DataRowView
        Dim Msg As StringBuilder
        'Dim i As Integer
        Dim res As MsgBoxResult
        Dim intCheckEachRow As Integer

        If Me.grdCases.SelectedItems.Count = 0 Then
            Exit Sub
        End If

        If Me.grdCases.SelectedItems.Count > 1 Then
            intCheckEachRow = MsgBox("You have selected multiple cases. Do you want to go mad and delete them all in one go, right now?", MsgBoxStyle.YesNoCancel)
        End If

        If intCheckEachRow = MsgBoxResult.Cancel Then
            Exit Sub
        End If

        For Each myRow In Me.grdCases.SelectedItems

            Msg = New StringBuilder

            If Me.grdCases.SelectedItems.Count = 1 Or intCheckEachRow = MsgBoxResult.No Then
                With Msg
                    .Append("Are you sure you wish to delete this lead: (")
                    .Append(myRow.Item("Case_ID").ToString & ",")
                    '.Append(myRow.Cells(" ").Value & ",")
                    .Append(myRow.Item("Nationality").ToString & ",")
                    .Append(myRow.Item("Product").ToString & ") ?")
                End With

                res = MsgBox(Msg.ToString, MsgBoxStyle.YesNoCancel)

                Select Case res
                    Case MsgBoxResult.Cancel
                        Exit For
                    Case MsgBoxResult.Yes
                        Admin.DeleteLead(myRow.Item("Case_ID").ToString)
                    Case MsgBoxResult.No

                End Select

            ElseIf intCheckEachRow = MsgBoxResult.Yes Then
                Admin.DeleteLead(myRow.Item("Case_ID").VaToStringlue)
            Else
                ' do nothing
            End If

        Next

        Me.LoadCases(False)



    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        LoadCases(True)
    End Sub

    Private Sub btnClose_Click_1(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub drpBranchCon_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpBranchCon.SelectionChanged

        With Me.drpConName
            .DisplayMemberPath = "con_name"
            .SelectedValuePath = "con_id"
            .ItemsSource = CType(CommonDataFunctions.ConsultantList(True, , , drpBranch.SelectedValue), IListSource).GetList
            .SelectedIndex = -1
        End With

    End Sub

    Private Sub drpDept_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles drpDept.SelectionChanged

    End Sub
End Class
